import { _decorator, Animation, Component, find, instantiate, Label, Layers, Node, Sprite, tween, Vec3 } from 'cc';
import DataManager from './DataManager';
import EventCenter from './EventCenter';
import ViewType from './ViewType';
import SoundManager from './SoundManager';
const { ccclass, property } = _decorator;

@ccclass('GameResult')
export class GameResult extends Component {
    @property(Node)
    protected mGoldNode:Node=null;
    @property(Node)
    protected mItemNode:Node=null;
    @property(Label)
    protected mGoldLabel:Label=null;
    private mIsFly:boolean;
    private mGlod:number=0;
    private mAddGlod:number=0;
    private mClickBtnIndex:number=0;
    protected onEnable(): void {
        this.mIsFly=false;
        this.playAnim();
    }

    public setData(glod:number){
        this.mAddGlod=glod;
        this.node.active=true;
        this.gold = DataManager.ins.user.gold;
        this.freshView();
    }

    private freshView(){
        this.mItemNode.active=true;
        this.mGoldLabel.string=this.gold.toString();
        find("ele/txt",this.mItemNode).getComponent(Label).string="x"+this.mAddGlod;
    }

    protected onHomeBtnClick(){
        if(this.mIsFly)return;
        this.mClickBtnIndex=0;
        this.fly();
    }

    protected onNextBtnClick(){
        if(this.mIsFly)return;
        this.mClickBtnIndex=1;
        this.fly();
    }

    private fly(){
        SoundManager.ins.playOneShot("collect");
        let ticon:Node = this.createSp();
        ticon.setPosition(this.mItemNode.position);
        ticon.parent=this.node;
        this.mItemNode.active=false;
        let tpos = this.mGoldNode.position.clone();
        tween(ticon).to(.5,{position:tpos}).call(()=>{
            ticon.removeFromParent();
            ticon.destroy();
           this.addGold();
        }).start();
    }
    
    private addGold(){
        SoundManager.ins.playOneShot("add");
        let tself:any=this;
        tween(tself).to(1,{gold:this.mGlod+this.mAddGlod}).call(()=>{
            tself.node.active=false;
            DataManager.ins.user.addGlod(tself.mAddGlod);
            DataManager.ins.saveUserData();
            EventCenter.getIns().emit(ViewType.close_view_reult,[this.mClickBtnIndex]);
        }).start();
    }

    private playAnim(){
        let tele = find("ele",this.mItemNode);
        let tanim = find("wAnim",this.mItemNode);
        let tanim2 = find("wAnim2",this.mItemNode);
        let tanim3 = find("wAnim3",this.mItemNode);
        tanim.active=false;
        tanim2.active=false;
        tanim3.active=false;
        tele.setScale(new Vec3());
        this.scheduleOnce(()=>{
            tanim.active=true;
            tanim2.active=true;
            tanim.getComponent(Animation).play();
            tanim2.getComponent(Animation).play();
            tanim.getComponent(Animation).once(Animation.EventType.FINISHED,()=>{
                tanim.active=false;
                tanim2.active=false;
            });
            tween(tele).to(0.5,{scale:new Vec3(1,1,1)}).call(()=>{
                tanim3.active=true;
            }).start()
        },0.5);
    }

    private createSp(){
        let tnode:Node=new Node();
        tnode.layer=Layers.Enum.UI_2D; 
        let tsp:Sprite = tnode.addComponent(Sprite);
        tsp.spriteFrame = find("ele",this.mItemNode).getComponent(Sprite).spriteFrame;
        return tnode;
    }

    public get gold():number{
        return this.mGlod;
    }

    public set gold(value:number){
        this.mGlod=value;
        this.mGoldLabel.string=Math.floor(this.mGlod).toString();
    }
}


